/**
 * 
 */
package edu.arizona.eller.mis.liferoulette;

import org.json.JSONException;
import org.json.JSONObject;

import edu.arizona.eller.mis.liferoulette.entity.ActivityBean;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;

/**
 * @author Blaine Boule - blaineb@email.arizona.edu
 *
 */
public class HistoryActivity extends ListActivity {
	@Override
	
	protected void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		super.onListItemClick(l, v, position, id);
		ActivityBean item = (ActivityBean) this.getListAdapter().getItem(position);
		try {
			final JSONObject currentItem = new JSONObject(item.getActivity());
			final String name = (String) currentItem.getString("name");
			final String vicinity = (String) currentItem.getString("vicinity");
			JSONObject placeGeo = currentItem.getJSONObject("geometry");
			JSONObject placeLoc = placeGeo.getJSONObject("location");
            final double lat = placeLoc.getDouble("lat");
            final double lon = placeLoc.getDouble("lng");
            
			Builder b = new AlertDialog.Builder(this).setMessage(vicinity).setTitle(name).setIcon(R.drawable.map);
			b.setPositiveButton("Direction", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int whichButton) {
					Intent i = new Intent(getApplicationContext(),result_maps.class);
					
					
		            i.putExtra("lat", lat);
					i.putExtra("lon", lon);
					i.putExtra("name",name);
					i.putExtra("vicinity", vicinity);
					startActivity(i);
				}
			}).create();
			b.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int whichButton) {
					dialog.cancel();
				}
			}).create();
			b.show();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	private HistoryAdapter adapter;
	 
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//setContentView(R.layout.history);
		adapter = new HistoryAdapter(this);
		setListAdapter(adapter);
	}
	 
	@Override
	/*protected void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		super.onListItemClick(l, v, position, id);
		Toast.makeText(this, "Click-" + String.valueOf(position),Toast.LENGTH_SHORT).show();
	}*/
	
	public boolean onCreateOptionsMenu(Menu menu) {
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.history_menu, menu);
	    return true;
	}
	
	public boolean onOptionsItemSelected(MenuItem item) {
	    switch (item.getItemId()) {
	        case R.id.settingsItem:     
	        	startActivity(new Intent(this, UserPreferenceAcvitity.class));
	            break;
	        case R.id.homeItem:     
	        	startActivity(new Intent(this, main.class));
	            break;
	    }
	    return true;
	}
}
